<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>添加用户信息</title>

</head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery/jquery.min.js"></script>
<script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
<script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
<style>
    input.error{
        border: 1px solid #E6594E;
    }
</style>
<body>
<div class="container">
    <form id="addUserForm">
        <br/>
        <div class="form-group">
            <label for="username">用户名称：</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label  for="password">用户密码：</label>
            <input type="text" id="password" name="password" class="form-control" placeholder="请输入密码">

        </div>
        <div class="form-group">
            <label for="age">年龄：</label>
            <input type="text" id="age" name="age" class="form-control" placeholder="请输入年龄">

        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>

<script>
    var addUser = function(){
        if(!check().form()){
            return;
        }
        $.ajax({
            type: "post",
            dataType: "json",
            url: "/add",
            data: {
                "username":$("#username").val(),
                "password":$("#password").val(),
                "age":$("#age").val()

            },
            success: function(msg){
                $('#cancelBtn').click();
            }
        });
    }

    $('#saveBtn').on('click', function(){
        addUser();
    });

    $('#cancelBtn').on('click', function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.getUserPageList();
        parent.layer.close(index);
    });

    //校验字段是否正确
    function check(){
        /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
        return $("#addUserForm").validate({
            rules:{
                username:{
                    required:true
                },
                password:{
                    required:true
                },
                age:{
                    required:true
                }
            },
            messages:{
                username:{
                    required:""
                },
                password:{
                    required:""
                },
                age:{
                    required:""
                }
            }
        });
    }

</script>
</body>
</html>